package com.bw.p19.login

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.p19.R
import com.bw.p19.databinding.ActivityUpdateBinding
@Route(path = "/app/UpdateActivity")
class UpdateActivity : BaseActivity<ActivityUpdateBinding,LoginViewModel>() {


    override fun initView() {
        val phone = SPUtils.getInstance().getString("phone")
        val replaceRange = phone.replaceRange(3, 7, "****")
        binding.tvPhone.text = "当前手机号:$replaceRange"
        //发送验证码
        binding.btnCode.setOnClickListener {
            viewModel.codeSend(phone)
        }
        //点击按钮，实现确认修改密码
        binding.btnUpdate.setOnClickListener {
            val etPwd = binding.etPwd.text.toString()
            val etRePwd = binding.etRepwd.text.toString()
            val etCode = binding.etCode.text.toString()
            if (etPwd.isEmpty()||etRePwd.isEmpty()||etCode.isEmpty()){
                ToastUtils.showLong("所有输入框不能为空")
                return@setOnClickListener
            }
            if (etPwd!=etRePwd){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            viewModel.updatePWd(phone,etCode,etPwd)
        }

    }

    override fun initData() {
        viewModel.codeSendSuccess.observe(this){
            if (it.code == 200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }
            ToastUtils.showLong(it.message)
        }
        viewModel.updatePwdSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if (it.code == 200){
                finish()
            }

        }
    }

    override fun getVideModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_update
    }
}